import {
    validIdentity,
    validPhone,
	validNumber,
	validInt,
	validSmallLength,
	validBigLength,
} from './validate'

import {
	checkUserName,
	checkTemplate,
	checkPwd,
} from '@/api/RCIM/RCIM-validate'

/**
 *
 * 校验是否是Int
 */
export const validateFormInt = (rule, value, callback) => {
	if (!validInt(value)) {
		callback(new Error("必须是整数"));
	} else {
		callback();
	}
};

/**
 *
 * 校验是否是number
 */
export const validateFormNumber = (rule, value, callback) => {
	if (!validNumber(value)) {
		callback(new Error("必须是数字"));
	} else {
		callback();
	}
};

/**
 *
 * 校验字符串长度是否小于某个值
 * @param { number } lengt 长度
 */
export const validateFormSmallLength = (lengt) => {
	return function (rule, value, callback) {
        value += ''
		if (!validSmallLength(value, lengt)) {
			callback(new Error("长度必须小于" + lengt));
		} else {
			callback();
		}
	}
}

/**
 *
 * 校验字符串长度是否小于某个值
 * @param { number } lengt 长度
 */
export const validateFormBigerLength = (lengt) => {
	return function (rule, value, callback) {
        value += ''
		if (!validBigLength(value, lengt)) {
			callback(new Error("长度必须大于" + lengt));
		} else {
			callback();
		}
	}
}

export const validateFormPhone = (rule, value, callback) => {
    if (!validPhone(value) && value !== '' ) {
        callback(new Error("请输入正确的手机号"));
    } else {
        callback();
    }
};

export const validFormIdentity = (rule, value, callback) => {
    if (!validIdentity(value) && value !== '') {
        callback(new Error("请输入正确的身份证号"));
    } else {
        callback();
    }
};

export const validFormUserNameIsUse = (rule, value, callback) => {
	checkUserName({
		userName: value
	}).then(res => {
		if (res.data === '1') {
			callback(new Error("当前账号已存在，请重新输入或直接登陆"));
		} else {
			callback();
		}
	})

}

export const validFormCheckPwd = (rule, value, callback) => {
	const storagevalue = window.localStorage.getItem('llqAuth');
	const userName = JSON.parse(storagevalue).username
	checkPwd({
		userName: userName,
		pwd: value
	}).then(res => {
		if (res.data === 2) {
			callback();
		} else {
			callback(new Error("密码错误，请确认旧密码"));
		}
	})

}

export const validFormTemplateIsUse = (rule, value, callback) => {
	checkTemplate({
		id: value
	}).then(res => {
		if (res.data === 1) {
			callback(new Error("当前报警类型的模板已存在，请前往编辑或直接使用"));
		} else {
			callback();
		}
	})

}
